package main

import (
	"context"
	"fmt"
	"log"
	"os"

	"github.com/olivere/elastic/v7"
)

type Account struct {
	Name    string    `json:"name"`
	Address string    `json:"address"`
	Age     int       `json:"age"`
	Company []Company `json:"company"`
}

type Company struct {
	Name    string `json:"name"`
	Address string `json:"address"`
}

func main() {
	url := "http://120.24.67.46:9200"
	// 打印elasticsearch的执行语句
	logger := log.New(os.Stdout, "myshop", log.LstdFlags)
	client, err := elastic.NewClient(
		elastic.SetURL(url),
		elastic.SetSniff(false),
		elastic.SetTraceLog(logger),
	)
	if err != nil {
		// Handle error
		panic(err)
	}

	account1 := Account{
		Name:    "Mikel",
		Address: "America New York",
		Age:     26,
		Company: []Company{
			{
				Name:    "Apple",
				Address: "Texas",
			},
			{
				Name:    "SpaceX",
				Address: "Los Angeles",
			},
		},
	}
	put1, err := client.Index().
		Index("myuser").
		// Id("1").
		BodyJson(account1).
		Do(context.Background())
	if err != nil {
		// Handle error
		panic(err)
	}
	fmt.Printf("Indexed account1 %s to index %s, type %s\n", put1.Id, put1.Index, put1.Type)

}
